package com.gr.index;

/**
 * Copyright Manning Publications Co.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific lan      
*/

import com.gr.analys.VietAnalyzer;
import com.gr.search.LocationRecord;
import com.gr.search.ManagerRecord;
import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;

import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;

import java.io.InputStreamReader;
import java.util.Random;

// From chapter 1

/**
 * This code was originally written for
 * Erik's Lucene intro java.net article
 */
public class IndexLocation {

   ManagerRecord manager = new ManagerRecord();
   

  public static void main(String[] args) throws Exception {
   /* if (args.length != 2) {
      throw new IllegalArgumentException("Usage: java " + Indexer.class.getName()
        + " <index dir> <data dir>");
    }*/
//    String result;
//    result = JOptionPane.showInputDialog("Hay nhap thu muc chua file can index:");
//    System.out.println("Nhap thu muc chua tai lieu:\n");
    String indexDir = "C:/Index";         //1
    String dataDir = "C:/CrawlerData";         //2

    long start = System.currentTimeMillis();
    IndexLocation indexer = new IndexLocation(indexDir);
    int numIndexed;
    try {
      numIndexed = indexer.index(dataDir, new TextFilesFilter());
    } finally {
      indexer.close();
    }
    long end = System.currentTimeMillis();

    System.out.println("Indexing " + numIndexed + " files took "
      + (end - start) + " milliseconds");
  }

  private IndexWriter writer;

  public IndexLocation(String indexDir) throws IOException {
    Directory dir = FSDirectory.open(new File(indexDir));
    System.out.println(dir.toString());
    writer = new IndexWriter(dir,            //3
                 new VietAnalyzer(),//3
                 true,                       //3
                             IndexWriter.MaxFieldLength.UNLIMITED); //3
//    writer.setInfoStream(System.out);
 //   writer.optimize();
   // writer.setUseCompoundFile(false);
  }

  public void close() throws IOException {
    writer.close();                             //4
  }

  public int index(String dataDir, FileFilter filter)
    throws Exception {

    File[] files = new File(dataDir).listFiles();

    for (File f: files) {
      if (!f.isDirectory() &&
          !f.isHidden() &&
          f.exists() &&
          f.canRead() &&
          (filter == null || filter.accept(f))) {
        indexFile(f);
      }
    }
    return writer.numDocs();                     //5
  }

  private static class TextFilesFilter implements FileFilter {
    public boolean accept(File path) {
      return path.getName().toLowerCase()        //6
             .endsWith(".txt");                  //6
    }
  }

  protected Document getDocument(File f) throws Exception {
	  Document doc = new Document(); 
      doc.add(new Field("contents", new InputStreamReader(new FileInputStream(f), "UTF-8")));      //7 
      doc.add(new Field("filename", f.getCanonicalPath(),     //8 
               Field.Store.YES, Field.Index.NOT_ANALYZED)); 
     
      int docID = manager.getLastDocID()+1;
      doc.add(new Field("docID",""+docID,Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
      Random generator = new Random();
      float k1 = generator.nextFloat()*359-180;
      float k2 = generator.nextFloat()*359-180;
      LocationRecord lc = new LocationRecord(k1,k2,docID);
      manager.insertLoc(lc);
      return doc; 
  }

  private void indexFile(File f) throws Exception {
    System.out.println("Indexing " + f.getCanonicalPath());
    Document doc = getDocument(f);
    writer.addDocument(doc);           
  }
}

/*
#1 Create index in this directory
#2 Index *.txt files from this directory
#3 Create Lucene IndexWriter
#4 Close IndexWriter
#5 Return number of documents indexed
#6 Index .txt files only, using FileFilter
#7 Index file content
#8 Index file name
#9 Index file full path
#10 Add document to Lucene index
*/

